<?php

namespace App\Http\Controllers\v1;

use App\Models\User;
use App\Models\Role;
use Illuminate\Http\Request;
use Illuminate\Http\Exception\HttpResponseException;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Http\Response as IlluminateResponse;
use JWTFactory;
use App\Http\Controllers\Controller;
use App\DB\MonitorDB;

class MonitorController extends Controller {
	/**
	 * Create a new controller instance.
	 *
	 * @return void
	 */
	protected $app;
	
	/**
	 *
	 * @param \Laravel\Lumen\Application $app        	
	 */
	public function __construct(\Laravel\Lumen\Application $app) {
		$this->app = $app;
	}
	
	/**
	 * 上传监测数据
	 *
	 * @param Request $request        	
	 */
	public function upload(Request $request) {
		$jsondata = $request->getContent ();
		$data = json_decode ( $jsondata );
		
		MonitorDB::addMonitorData ( $request->user ()->uid, $data );
		return responseError ();
	}
	
	/**
	 * 下载某设备监测数据
	 *
	 * @param Request $request        	
	 */
	public function download(Request $request, $device_no, $start_date, $end_date) {
		$page = $request->has ( 'page' ) ? $request->input ( 'page' ) : 1;
		$size = $request->has ( 'size' ) ? $request->input ( 'size' ) : 5;
		$monitorData = MonitorDB::getMonitorData ( $device_no, $start_date, $end_date, $page, $size );
		return responseListData ( $monitorData, MonitorDB::getMonitorDataCount ( $device_no, $start_date, $end_date ) );
	}
	
	/**
	 * 获取某设备最近上传的count条记录
	 *
	 * @param Request $request        	
	 * @param unknown $device_no        	
	 * @param unknown $count        	
	 */
	public function downloadLatest(Request $request, $device_no) {
		$page = $request->has ( 'page' ) ? $request->input ( 'page' ) : 1;
		$size = $request->has ( 'size' ) ? $request->input ( 'size' ) : 5;
		$monitorData = MonitorDB::getLatestMonitorData ( $device_no, $page, $size );
		return responseListData ( $monitorData, MonitorDB::getLatestMonitorDataCount ( $device_no ) );
	}
	/**
	 * 获取预警数据
	 *
	 * @param Request $request        	
	 * @param unknown $device_no        	
	 * @param unknown $start_date        	
	 * @param unknown $end_date        	
	 * @param unknown $marked        	
	 */
	public function downloadDanger(Request $request, $device_no, $start_date, $end_date, $marked) {
		$page = $request->has ( 'page' ) ? $request->input ( 'page' ) : 1;
		$size = $request->has ( 'size' ) ? $request->input ( 'size' ) : 5;
		$dangerData = MonitorDB::getDangerData ( $device_no, $start_date, $end_date, $page, $size, $marked );
		return responseListData ( $dangerData, MonitorDB::getDangerDataCount ( $device_no, $start_date, $end_date, $marked ) );
	}
	
	/**
	 * 获取某个用户下设备的所有预警数据
	 *
	 * @param Request $request        	
	 * @param unknown $start_date        	
	 * @param unknown $end_date        	
	 */
	public function downloadUserDanger(Request $request, $start_date, $end_date) {
		$page = $request->has ( 'page' ) ? $request->input ( 'page' ) : 1;
		$size = $request->has ( 'size' ) ? $request->input ( 'size' ) : 5;
		$dangerData = MonitorDB::getUserDangerData ( $request->user ()->uid, $start_date, $end_date, $page, $size );
		return responseListData ( $dangerData, MonitorDB::getUserDangerDataCount ( $request->user ()->uid, $start_date, $end_date ) );
	}
	
	/**
	 * 更新预警数据是否被查看标记
	 *
	 * @param Request $request        	
	 * @return
	 *
	 */
	public function marked(Request $request) {
		$jsondata = $request->getContent ();
		$data = json_decode ( $jsondata );
		
		MonitorDB::marked ( $data->id );
		return responseError ();
	}
	
	/**
	 * 获取通知数据条数
	 * 
	 * @param Request $request        	
	 */
	public function notify(Request $request) {
		return responseData ( MonitorDB::getNotifyDataCount ( $request->user ()->uid ) );
	}
}
